#!/usr/bin/env python
# -*- coding: utf-8 -*-

__all__ = ['Global']
__version__ = '$Id$'

import os

from jsCSS.processor import Processor

import PIL.Image

from PyV8 import JSClass

class Drawing(JSClass):
    """jsCSS Drawing namespace"""

    class Point(JSClass):
        """Basic 2-dimensional point"""
        def __init__(self):
            self.x, self.y = 0, 0

        def __str__(self):
            return '<jsCSS.context.Drawing.%s>' % (self.__class__.__name__, )


    def get_image_size(self, path):
        size = Drawing.Point()
        image = PIL.Image.open(path)
        (size.x, size.y, ) = image.size
        return size

    def __str__(self):
        return '<jsCSS.context.%s>' % (self.__class__.__name__, )


class Global(JSClass):
    """jsCSS Global namespace"""

    class Processor(Processor):

        def pre_process(self, document, string):
            self._reset()
            return self._insert_string(string, 0, '''<?

var jsCSS = {
    drawing: {
        getImageSize: _jsCSS_drawing.get_image_size
    }
};

?>''')

        def __str__(self):
            return '<jsCSS.context.Global.%s>' % (self.__class__.__name__, )


    def __init__(self):
        self._jsCSS_drawing = Drawing()
        self.processor = Global.Processor()

    def __str__(self):
        return '<jsCSS.context.%s>' % (self.__class__.__name__, )
